package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.MarketBrandService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MarketBrandServiceImpl implements MarketBrandService {
    @Autowired
    MarketBrandMapper brandMapper;
    @Override
    public List<MarketBrand> list(int page, int limit, String sort, String order, int id, String name) {
        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = brandExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (id != 0) {
            criteria.andIdEqualTo(id);
        }

        criteria.andDeletedEqualTo(false);
        //设置排序方式
        brandExample.setOrderByClause(sort + " " + order);
        //设置分页
        PageHelper.startPage(page, limit);

        List<MarketBrand> marketBrands = brandMapper.selectByExample(brandExample);

        return marketBrands;
    }

    @Override
    public int create(MarketBrand brand) {
        int insert = brandMapper.insert(brand);
        return insert;
    }

    @Override
    public int update(MarketBrand brand) {
        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        int resCode = brandMapper.updateByExampleSelective(brand, brandExample);
        return resCode;
    }
}
